﻿using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.ComponentModel;
using System.Windows.Forms;
using System.Drawing;

namespace eComet
{
    public class ECometPlugIn
    {
        private ECometStatus status;
        private uint hMenu = 0;
        private uint iMenu = 0;
        private int startId = 0;
        private int id = 0;
        private uint hmnuPopup = 0;
        private uint elements = 0;
        private uint startPos = 0;
        public Dictionary<int, List<String>> commands = new Dictionary<int, List<string>>();

        public virtual ECometConfig Configuration()
        {
            return new ECometConfig("Test");
        }

        public int PrivateStart(uint hMenu, uint iMenu, int startId, uint numFiles, int typeDirOrFile, List<String> paths, List<String> extensions,int startPos,ECometConfig config,ref uint submenu, bool exist)
        {
            int exit = 0;
            this.hMenu = hMenu;
            this.iMenu = iMenu;
            this.startId = startId;
            this.id = startId;
            this.startPos = ((uint)startPos);
            if (!config.ApplicationName.Equals(String.Empty) && !exist)
            {
                    uint hmnuPopup = submenu = Helpers.CreatePopupMenu();
                    MENUITEMINFO mii = new MENUITEMINFO();
                    mii.cbSize = 48;
                    mii.fMask = (uint)MIIM.TYPE | (uint)MIIM.STATE | (uint)MIIM.SUBMENU;
                    mii.hSubMenu = (int)hmnuPopup;
                    mii.fType = (uint)MF.STRING;
                    mii.dwTypeData = config.ApplicationName;
                    mii.fState = (uint)MF.ENABLED;
                    Helpers.InsertMenuItem(hMenu, this.startPos, 1, ref mii);
                    this.startPos++;
                    this.hMenu = hmnuPopup;
            }
            return Menu(paths, extensions, numFiles, typeDirOrFile);
        }

        public Dictionary<string, List<string>> GroupFilesByExtension(List<String> paths)
        {
            Dictionary<string, List<string>> archivos = new Dictionary<string, List<string>>();
            foreach (String archivo in paths)
            {
                if (!IsDirectory(archivo))
                {
                    string extension = Path.GetExtension(archivo);
                    if (!archivos.ContainsKey(extension))
                    {
                        archivos[extension] = new List<string>();
                    }
                    archivos[extension].Add(archivo);
                }
                else
                {
                    string extension = "directory";
                    if (!archivos.ContainsKey(extension))
                    {
                        archivos[extension] = new List<string>();
                    }
                    archivos[extension].Add(archivo);
                }
            }
            return archivos;
        }



        public bool IsDirectory(string file)
        {
            FileAttributes attr = File.GetAttributes(file);
            //detect whether its a directory or file
            if ((attr & FileAttributes.Directory) == FileAttributes.Directory)
                return true;
            else
                return false;
        }

        public uint CreatePopupMenu()
        {
            return Helper.CreatePopupMenu();
        }


        public int AddMenu(String text)
        {
            return AddMenu(text,(int) hMenu);
        }

        public int AddMenu(String text,Bitmap bmp)
        {
            IntPtr ip = bmp.GetHbitmap();
            uint hmnuPopup = Helpers.CreatePopupMenu();
            MENUITEMINFO mii = new MENUITEMINFO();
            mii.cbSize = 48;
            mii.fMask = (uint)MIIM.TYPE | (uint)MIIM.STATE | (uint)MIIM.SUBMENU | (uint)MIIM.CHECKMARKS;
            mii.hSubMenu = (int)hmnuPopup;
            mii.fType = (uint)MF.STRING;
            mii.dwTypeData = text;
            mii.fState = (uint)MF.ENABLED;
            mii.hbmpChecked = (int)ip;
            mii.hbmpItem = (int)ip;
            mii.hbmpUnchecked = (int)ip;
            Helpers.InsertMenuItem(hMenu, startPos + elements, 1, ref mii);
            //elements++;
            return (int)hmnuPopup;
        }

        public int AddMenu(String text,int subMenu)
        {
            uint hmnuPopup = Helpers.CreatePopupMenu();
            MENUITEMINFO mii = new MENUITEMINFO();
            mii.cbSize = 48;
            mii.fMask = (uint)MIIM.TYPE | (uint)MIIM.STATE | (uint)MIIM.SUBMENU;
            mii.hSubMenu = (int)hmnuPopup;
            mii.fType = (uint)MF.STRING;
            mii.dwTypeData = text;
            mii.fState = (uint)MF.ENABLED;
            Helpers.InsertMenuItem((uint)subMenu, startPos + elements, 1, ref mii);
            //elements++;
            return (int)hmnuPopup;
        }

        public int AddMenu(String text, int subMenu,Bitmap bmp)
        {
            IntPtr ip = bmp.GetHbitmap();
            uint hmnuPopup = Helpers.CreatePopupMenu();
            MENUITEMINFO mii = new MENUITEMINFO();
            mii.cbSize = 48;
            mii.fMask = (uint)MIIM.TYPE | (uint)MIIM.STATE | (uint)MIIM.SUBMENU | (uint)MIIM.CHECKMARKS;
            mii.hSubMenu = (int)hmnuPopup;
            mii.fType = (uint)MF.STRING;
            mii.dwTypeData = text;
            mii.fState = (uint)MF.ENABLED;
            mii.hbmpChecked = (int)ip;
            mii.hbmpItem = (int)ip;
            mii.hbmpUnchecked = (int)ip;
            Helpers.InsertMenuItem((uint)subMenu, startPos + elements, 1, ref mii);
            //elements++;
            return (int)hmnuPopup;
        }

        public int AddMenuItem(String text, int subMenu, Bitmap bmp)
        {
            IntPtr ip = bmp.GetHbitmap();
            MENUITEMINFO mii = new MENUITEMINFO();
            mii.cbSize = 48;
            mii.fMask = (uint)MIIM.ID | (uint)MIIM.TYPE | (uint)MIIM.STATE | (uint)MIIM.CHECKMARKS;
            mii.wID = id;
            mii.fType = (uint)MF.STRING;
            mii.dwTypeData = text;
            mii.fState = (uint)MF.ENABLED;
            mii.hbmpChecked = (int)ip;
            mii.hbmpItem = (int)ip;
            mii.hbmpUnchecked = (int)ip;
            Helpers.InsertMenuItem((uint)subMenu, startPos + elements, 1, ref mii);
            id++;
            elements++;
            return (int)(elements - 1);
        }

        public int AddMenuItem(String text, Bitmap bmp)
        {
            return AddMenuItem(text, (int)hMenu,bmp);
        }


        public int AddMenuItem(String text, String commandPath, String commandArgs,Bitmap bmp)
        {
            return AddMenuItem(text, commandPath, commandArgs, (int)hMenu, bmp);
        }

        public int AddMenuItem(String text, String commandPath, String commandArgs, int subMenu,Bitmap bmp)
        {
            List<String> command = new List<String>();
            command.Add(commandPath);
            command.Add(commandArgs);
            commands.Add((int)elements, command);
            return AddMenuItem(text, subMenu, bmp);
        }


        public int AddMenuItem(String text,int subMenu)
        {
            MENUITEMINFO mii = new MENUITEMINFO();
            mii.cbSize = 48;
            mii.fMask = (uint)MIIM.ID | (uint)MIIM.TYPE | (uint)MIIM.STATE;
            mii.wID = id;
            mii.fType = (uint)MF.STRING;
            mii.dwTypeData = text;
            mii.fState = (uint)MF.ENABLED;
            Helpers.InsertMenuItem((uint)subMenu, startPos + elements, 1, ref mii);
            id++;
            elements++;
            return (int)(elements - 1);
        }

        public int AddMenuItem(String text)
        {
            return AddMenuItem(text,(int)hMenu);
        }



        public int AddMenuItem(String text,String commandPath,String commandArgs)
        {
            return AddMenuItem(text, commandPath, commandArgs,(int) hMenu);
        }

        public int AddMenuItem(String text, String commandPath, String commandArgs,int subMenu)
        {
            List<String> command = new List<String>();
            command.Add(commandPath);
            command.Add(commandArgs);
            commands.Add((int)elements, command);
            return AddMenuItem(text, subMenu);
        }

        public int RunCommand(string command, string args)
        {
            var proc = new Process();
            proc.StartInfo.WindowStyle = ProcessWindowStyle.Normal;
            proc.StartInfo.FileName = command;
            if (args != String.Empty)
            {
                proc.StartInfo.Arguments = args;
            }

            try
            {
                proc.Start();
                //proc.WaitForExit();
            }
            catch (Win32Exception e)
            {
                return -1;
            }

            //return proc.ExitCode;
            return 0;
        }


        public int RunCommand(int itemId)
        {
            int exit = 0;
            if (commands.ContainsKey(itemId))
            {
                List<String> lstCommand = commands[itemId];
                String command = lstCommand[0];
                String args = lstCommand[1];
                var proc = new Process();
                proc.StartInfo.WindowStyle = ProcessWindowStyle.Normal;
                proc.StartInfo.FileName = command;
                if (args != String.Empty)
                {
                    proc.StartInfo.Arguments = args;
                }

                try
                {
                    proc.Start();
                    exit = 1;
                    //proc.WaitForExit();
                }
                catch (Win32Exception e)
                {
                    exit = -1;
                }

                //return proc.ExitCode;
            }
            return exit;
        }

        public String getText()
        {
            String exit = "";
            return exit;
        }

        public int ReturnStatus()
        {
            return (int)elements;
        }

        public virtual int Menu(List<String> paths, List<String> extensions, uint numFiles, int typeDirOrFile)
        {
            int exit = 0;
            return exit;
        }

        public virtual void Actions(int selectedId)
        {

        }
    }
}
